<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Country_Model Class
 * 
 * Handles all database requests regarding the ae_country table.
 * 
 * -----------------------------------------------------------------------------
 * Copyright (C) 2008   Patrick Clark (pubblichi.com/blog)
 * -----------------------------------------------------------------------------
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ------------------------------------------------------------------------------
 * @package     AuthorEyes
 * @subpackage  Models
 * @category    Authentication
 * @author      Patrick Clark (Tap)
 * @copyright   Copyright (c) 2008, pubblichi.com
 * @license     http://www.gnu.org/licenses/lgpl.html
 * @link        http://www.pubblichi.com/authoreyes
 * @info        This is an adaptation of the FreakAuth Light authentication library,
 * @info        written specifically for Kohana.
 * @info     	FreakAuth Light written by Daniel Vecchiato (danfreak) & Christophe Gragnic (grahack)
 * @info        Visit www.4webby.com/freakauth to download FreakAuth Light for CodeIgniter
 * @version     0.1
 *
 */
class Country_Model extends Model {
	/*-------------+
	|  PROPERTIES  |
	+-------------*/
	// Table prefix.
	protected $prefix = '';
	// Table name.
	protected $table;
	
	/*----------+
	|  METHODS  |
	+----------*/
	/**
	 * Initializes the class.
	 * 
	 * @return void
	 */
	public function __construct()
	{
		// Load database library into $this->db.
		parent::__construct();
		
		// AuthorEyes table prefix.
		$this->prefix = Config::item('authoreyes.ae_table_prefix');
		$this->table = $this->prefix.'user';
	}
	
	/**
	 * Retrieves the countries in the table ae_country for use in a drop down option box.
	 * 
	 * @return array
	 */
	public function get_countries_for_select()
	{
		// Building query...
		$this->db->select('id', 'name');
		
		// Retrieve results...
		$query = $this->db->get($this->table);
		
		// Start building the options array.
		$options[0] = "-----------------";
		
		foreach ($query as $row)
		{
			$options[$row->id] = $row->name;
		}
		
		return $options;
	}
	
	/**
	 * Retrieves a country based on it's ID.
	 * 
	 * @param  Country ID ($id).
	 * @return string
	 */
	public function get_country_by_id($id)
	{
		// Building query...
		$this->db->select('name');
		$this->db->where('id', $id);
		
		// Assigns a query object.
		$query = $this->db->get($this->table);
		
		// Check to see if the result exists and returns result object, otherwise FALSE.
		if ($query->count() > 0)
		{
			return $query;
		}
		else
		{
			return FALSE;
		}
	}
}
?>
